package com.esb.transport.channel;

import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;

import com.esb.transport.protocal.Invocation;

public class NettyChannel extends AbstractChannel {

	private static final ConcurrentMap<io.netty.channel.Channel, Channel> channels = new ConcurrentHashMap<io.netty.channel.Channel, Channel>();
	
	private final io.netty.channel.Channel channel;
	
	public NettyChannel(io.netty.channel.Channel channel) {
		this.channel = channel;
	}
	
	@Override
	protected void doSend(Invocation message) {
		channel.write(message);
	}

	public static Channel wrapperChannel(io.netty.channel.Channel channel) {
		Channel ch = channels.get(channel);
		if(ch==null){
			Channel nc = new NettyChannel(channel);
			ch = channels.putIfAbsent(channel, nc);
			if(ch==null)ch = nc;
		}
		return ch;
	}

}
